package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.SeckillOrder;
import cn.wolfcode.shop.mapper.SeckillOrderMappaer;
import cn.wolfcode.shop.service.ISeckillOrderService;
import cn.wolfcode.shop.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {
    @Autowired
    private SeckillOrderMappaer seckillOrderMappaer;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public SeckillOrder findBySeckillIdAndUserId(Long seckillId, Long userId) {
        return seckillOrderMappaer.selectBySeckillIdAndUserId(seckillId, userId);
    }

    // 创建订单
    @Override
    public void creatSeckillOrder(Long seckillId, Long userId, String orderNo) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderNo(orderNo);
        seckillOrder.setSeckillId(seckillId);
        seckillOrder.setUserId(userId);
        seckillOrderMappaer.insert(seckillOrder);

        // 订单创建成功之后，将当前用户存进Redis的set集合中，用做重复下单的判断
        redisTemplate.opsForSet().add(RedisKeys.SECKILL_SUCCESS_USER_SET.join(
                seckillId + ""), userId + "");
    }
}
